package com.platform.upms.controller;

import com.platform.common.config.Global;
import com.platform.common.page.Page;
import com.platform.common.utils.MD5;
import com.platform.common.utils.UploadImageToString;
import com.platform.common.utils.UserUtils;
import com.platform.upms.model.UpmsOrg;
import com.platform.upms.model.UpmsPicture;
import com.platform.upms.model.UpmsUser;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsOrgService;
import com.platform.upms.service.UpmsPictureService;
import com.platform.upms.service.UpmsUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UpmsUserService upmsUserService;

    @Autowired
    private UpmsPictureService upmsPictureService;

    @Autowired
    private UpmsOrgService upmsOrgService;

    /**
     * 用户列表
     * @param model
     * @param page
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(Model model, Page<UpmsUser> page,UpmsUser upmsUser) {
        if(upmsUser.getSearch() !=null &&  "男".equals(upmsUser.getSearch())){
            upmsUser.setSex("1");
        }else if(upmsUser.getSearch() !=null&&  "女".equals(upmsUser.getSearch())){
            upmsUser.setSex("0");
        }else if(upmsUser.getSearch() !=null&&  "可用".equals(upmsUser.getSearch())){
            upmsUser.setStatus(1);
        }else if(upmsUser.getSearch() !=null&&  "禁用".equals(upmsUser.getSearch())){
            upmsUser.setStatus(0);
        }
        System.out.println(upmsUser);
        Page<UpmsUser> p = upmsUserService.findByPage(page,upmsUser);
        model.addAttribute("page", p);
        return "/user/list.jsp";
    }

    /**
     * 进入添加页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return "/user/add.jsp";
    }

    /**
     * 用户保存
     * @param user
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Tip save(UpmsUser user) {
        if(user.getOrgId() == null || "".equals(user.getOrgId())) {
            user.setOrgId(UserUtils.getUserOrgId());
        }
        user.setCreateTime(new Date());
        user.setLastLogin(new Date());
        user.setAvailableSpace(user.getTotalSpace());
        user.setPassword(MD5.md5(user.getPassword()));
        upmsUserService.save(user);
        return new Tip();
    }

    /**
     * 进入修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(@Param(value = "id") Integer id, Model model) {
        UpmsUser upmsUser = upmsUserService.findById(id);
        model.addAttribute("user", upmsUser);
        return "/user/edit.jsp";
    }

    /**
     * 用户基本信息修改
     * @param user
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Tip edit(UpmsUser user) {
        Tip tip = new Tip();
        try {
            if(!"".equals(user.getPassword()) && user.getPassword() !=null){
                user.setPassword(MD5.md5(user.getPassword()));
            }
            upmsUserService.edit(user);
        } catch (Exception e) {
            tip = new Tip(1);
        }
        return tip;
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Tip delete(Integer[] ids) {
        Tip tip = new Tip();
        try {
            upmsUserService.deleteByIds(ids);
        } catch(Exception e) {
            tip = new Tip(1);
        }
        return tip;
    }

    /**
     * 编辑头像页面
     * @return
     */
    @RequestMapping("/editHead")
    public String  editHead(ModelMap modelMap,HttpSession session){
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer id = upmsUser.getId();//当前用户的ID
        UpmsUser user = upmsUserService.get(id);
        modelMap.put("user",user);
        return "/user/editHead.jsp";
    }

    /**
     * 选择图片
     * @return
     */
    @RequestMapping("/chooseImg")
    public String chooseImg(HttpSession session, ModelMap modelMap,Page page){
       UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer id = upmsUser.getId();//当前用户的ID
        UpmsPicture upmsPicture=new UpmsPicture();
        upmsPicture.setUserId(id);
        Page<UpmsPicture> pictureList = upmsPictureService.findByPage(page,upmsPicture);
        modelMap.addAttribute("pictureList",pictureList);
        return  "/user/imgList.jsp";
    }


    /**
     * 修改图片
     * @param imgUrl
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/imgCut")
    @ResponseBody
    public Tip imgCut(String imgUrl,HttpServletRequest request,HttpSession session) {
        try{
            UpmsUser upmsUser = (UpmsUser) session.getAttribute(Global.UPMSUSER);
            Integer sessionUserId=upmsUser.getId();
            String urlStr= UploadImageToString.headImageToString(imgUrl,request,sessionUserId);
            upmsUser.setHeadUrl(urlStr);
            upmsUserService.update(upmsUser);
           return  new Tip("头像修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"头像修改失败");
        }

    }

    @RequestMapping(value = "/orgList")
    public String orgList(Page<UpmsOrg> page, Model model) {
        Page<UpmsOrg> list = upmsOrgService.getHighOrgList(page,null);
        model.addAttribute("page", list);
        return "/user/org-list.jsp";
    }

}
